/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.commands;

import controller.Constants;
import model.graphic.objects.GBoxNode;
import model.graphic.objects.GNode;
import model.graphic.objects.GTransmitterNode;
import model.graphic.objects.Graphical;
import model.graphic.objects.RectangleGraphical;
import model.graphic.objects.RectangleAttachment;

/**
 *
 * @author Jelle, modified by Ben
 */
public class ShrinkCommand extends BasicCommand {

    private Constants cons = new Constants();
    private RectangleGraphical base;

    public ShrinkCommand(RectangleGraphical base) {
        this.base = base;
    }

    @Override
    public void execute() {
        
        if (GNode.class.isAssignableFrom(base.getClass())) {
            GNode node = (GNode) base;
            boolean collapse = node.getShrinkAddon().isShrinked();
            node.getShrinkAddon().setShrinked(!collapse);
            for (Graphical g : gmodel.getAttachedObjects(node, cons.getPortsString())) {
                RectangleAttachment handle = (RectangleAttachment) g;
                if(handle.isVisible() && !handle.isCollapsed())
                    handle.collapse(true);
                else if(!handle.isVisible() && handle.isCollapsed()){
                    handle.collapse(false);
                }
            }
        } else if (GBoxNode.class.isAssignableFrom(base.getClass())) {
            GBoxNode node = (GBoxNode) base;
            boolean collapse = node.getShrinkAddon().isShrinked();
            node.getShrinkAddon().setShrinked(!collapse);
            for (GTransmitterNode g : gmodel.getAttachedTransmitterNodes(node)) {
                for (Graphical gh : gmodel.getAttachedObjects(g, cons.getPortsString())) {
                    RectangleAttachment ga = (RectangleAttachment)gh;
                    ga.collapse(!collapse);
                }
            }
        }
    }


}
